package com.shop.controller;

import com.shop.common.ResponseResult;
import com.shop.entity.Attribute;
import com.shop.service.AttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class AttributeController {
    @Autowired
    AttributeService attributeService;

    @ResponseBody
    @GetMapping("categories/{id}/attributes")
    public ResponseResult getList(@PathVariable("id")Integer id,
                                   @RequestParam String sel
    ){
        List<Attribute> list=attributeService.getList(id,sel);
        ResponseResult result=new ResponseResult();
        result.setSuccess("获取成功",list);
        return result;
    }
    @ResponseBody
    @PostMapping("categories/{id}/attributes")
    public ResponseResult add(@PathVariable("id")Integer id,
                              @RequestBody Attribute attribute
    ){
        attribute.setCat_id(id);
        ResponseResult result=new ResponseResult();
//        if (attribute.getAttr_sel()=="many"){
            int n=attributeService.add(attribute);
//        }else {
//            int n=attributeService.adds(attribute);
//        }

//        Attribute attribute1=attributeService.selectByAtt(attribute);
        result.setCREATED("创建成功",attribute);
        return result;
    }


    @DeleteMapping("/categories/{id}/attributes/{attrid}")
    public ResponseResult delete(@PathVariable("id")Integer id,
                                 @PathVariable("attrid")Integer attrid){
        ResponseResult result=new ResponseResult();
        Attribute attribute=attributeService.findByIds(id,attrid);
        if (attribute!=null){
            int delete=attributeService.delete(id,attrid);
            result.setDELETED("删除成功!");
        }else {
            result.setBADREQUEST("删除失败！");
        }
        return result;
    }
    @GetMapping("/categories/{id}/attributes/{attrId}")
    public ResponseResult findById(@PathVariable("id")Integer id,
                                   @PathVariable("attrId")Integer attrId,
                                   @RequestParam(value = "attr_sel",required = true) String attr_sel,
                                   @RequestParam(value = "attr_vals",required = false)String attr_vals
                                   ){
        Attribute attribute=attributeService.findOne(id,attrId,attr_sel);
        ResponseResult result=new ResponseResult();
        result.setSuccess("获取成功",attribute);
        return result;
    }

    @ResponseBody
    @PutMapping("/categories/{id}/attributes/{attrId}")
    public ResponseResult update(@PathVariable("id")Integer id,
                                 @PathVariable("attrId")Integer attrId,
                                 @RequestBody Attribute attribute){
        ResponseResult result=new ResponseResult();
        attribute.setCat_id(id);
        attribute.setAttr_id(attrId);
        String zz=attribute.getAttr_vals();
        if (attribute.getAttr_vals()==null){
            int n=attributeService.update(attribute);
        }else {
            int x=attributeService.updateVals(attribute);
        }
        Attribute attribute1 = attributeService.findByIds(id, attrId);
        result.setSuccess("更新成功",attribute1);
        return result;
    }

}
